package com.apes.scm.account.res.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.scm.account.res.model.Currency;
import com.apes.scm.account.res.repository.CurrencyRepository;
import com.apes.scm.masterdata.product.model.ProductUom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;

@Service("currencyService")
public class CurrencyService extends DomainService {
    @Autowired
    CurrencyRepository currencyRepository;

    public Currency save(SimpleRequest request) {
        Currency currency = request.getO(Currency.class);
        if(currency.getRateIds().size() == 0) {
            currency.setRateIds(new HashSet<>());
        }
        return currencyRepository.saveAndFlush(currency);
    }


    public String delete(SimpleRequest simpleRequest) {
        Currency currency = simpleRequest.getO(Currency.class);
        currencyRepository.delete(currency);
        return "删除成功！";
    }

    @Listener(topic = "event:account.currency.queryDecimalPlaces")
    public int computePrice(SimpleRequest request) {
        Currency currency = request.get("currency");
        return currency.queryDecimalPlaces();
    }
}
